<template>
  <!-- 延期归档申请 -->
  <base-breadcrumb class="archiveApply-container">
    <searchData :searchData="searchArr" @search="handleSearch" @autoBranchSelected="autoBranchSelected" @autoSelected="handleProjectAuto"></searchData>
    <a-card :bordered="false">
      <!-- <title-name title="延期归档申请" /> -->
      <!-- <div class="pub-table-action-btn">

      </div> -->
      <list-table
          ref="table"
          row-key="id"
          :columns="columns"
          :request="loadData"
          :alert="true"
          show-size-changer
          show-quick-jumper
      >
        <a-button
          slot="prefix"
          slot-scope="ref"
          v-btnPermission="'oa_008_add_01'"
          type="primary"
          :loading="loadingState.isAdd"
          @click="handleUpdate({}, 'add')">延期归档申请</a-button>
        <template slot="auditStatus" slot-scope="text, record">
          <status-display :statusCode="text" isApproval />
        </template>
        <template slot="action" slot-scope="text, record">
          <MoreButton>
            <a v-btnPermission="'oa_008_loo_02'" @click="handleUpdate(record, 'look')">查看</a>
            <!-- 只能编辑、删除自己创建的数据（v-selfBtn） -->
            <a v-if="(record.auditStatus == 0 || record.auditStatus == 3)&&selfBtnShow(record.createUserId)"  @click="handleUpdate(record, 'edit')">编辑</a>
            <a v-if="record.auditStatus && record.auditStatus != 0" @click="handleUpdate(record, 'auditInfo')">审批信息</a>
            <a v-if="record.auditStatus == 0&&selfBtnShow(record.createUserId)" @click="handleUpdate(record, 'delete')">删除</a>
          </MoreButton>
        </template>
      </list-table>
    </a-card>
  </base-breadcrumb>
</template>

<script>
import { STable } from '@/components' // 表格
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目
import { packageConfirm } from '@/utils/baseAction.js'
import StatusDisplay from '@/components/StatusDisplay'
import OrgTreeSelect from '@/components/OrgTreeSelect'
import { getDelayApplyList, deleteDelayApply, accessIfDelayApply } from '@/api/office/archiveManage/archiveApply.js'
import Vue from 'vue'

const userInfo = Vue.getAcloudProvider().getUserInfo()

const searchArr = [
  {
    name: '所属单位',
    valueKey: 'searchQuery',
    type: OrgTreeSelect,
  },
  // {
  //   name: '所属分公司',
  //   valueKey: 'branchOrgId',
  //   type: SelectSubCompany,
  //   autoSelected: userInfo?.currentPost?.code !== 'CO' // 分公司级别的岗位，自动选择
  // },
  // {
  //   name: '所属项目',
  //   keyValue: 'branchOrgId',
  //   valueKey: 'projectId',
  //   type: SelectProject,
  //   onlyCurrentPd: true,
  //   autoSelected: userInfo?.currentPost?.code === 'PD' // 项目级别的岗位，自动选择
  // },
]
const columns = [
  {
    title: '序号',
    width: 80,
    dataIndex: 'index',
    customRender() {
      return arguments[2] + 1
    },
  },
  {
    title: '分公司',
    dataIndex: 'branchOrgName',
  },
  {
    title: '项目',
    dataIndex: 'projectName',
  },
  {
    title: '计划竣工时间',
    dataIndex: 'completionTime',
  },
  {
    title: '原计划归档时间',
    dataIndex: 'archiveTime',
  },
  {
    title: '延期后归档时间',
    dataIndex: 'extendedArchiveTime',
  },
  {
    title: '状态',
    dataIndex: 'auditStatus',
    scopedSlots: { customRender: 'auditStatus' }
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '180px',
    align: 'center',
    scopedSlots: { customRender: 'action' },
  },
]
export default {
  name:'a'+Date.now(),
  components: {
    STable,
    StatusDisplay,
  },
  data() {
    return {
      loadingState: {
        isAdd: false
      },
      searchArr,
      columns: columns,
      queryParam: {},
    }
  },
  methods: {
    handleProjectAuto(val) {
      this.queryParam = {
        ...this.queryParam,
        projectId: val
      }
      if(userInfo?.currentPost?.code === 'PD') {
        this.refreshTable()
      }
    },
    autoBranchSelected(val) {
      this.queryParam = {
        ...this.queryParam,
        brandOrgId: val
      }
      if(userInfo?.currentPost?.code === 'BC') {
        this.refreshTable()
      }
    },
    /**
     * 处理数据的相关操作（增删改查）
     */
    handleUpdate(data, pageType) {
      const commonHandler = () => {
        this.$router.push({
          path: '/conference/archiveManage/archiveApply/update',
          query: {
            id: data.id || '',
            pageType: pageType,
          },
        })
      }
      const actionMap = {
        add: async () => {
          try {
            if(this.loadingState.isAdd) return
            this.loadingState.isAdd = true
            await accessIfDelayApply()
            commonHandler()
            this.loadingState.isAdd = false
          } catch(e) {
            console.error(e)
            this.loadingState.isAdd = false
          }
        },
        edit: commonHandler,
        look: commonHandler,
        auditInfo: () => {
          this.$bus.$emit('showApprovalDetailsDialog', data.id)
        },
        delete: async () => {
          packageConfirm({
            callback: async () => {
              const res = await deleteDelayApply({ id: data.id })
              this.$message.success('删除成功')
              this.refreshTable()
            },
          })
        },
      }
      actionMap[pageType] && actionMap[pageType]()
    },
    handleSearch(val = {}) {
      this.queryParam = {
        ...val,
      }
      this.refreshTable()
    },
    refreshTable() {
      this.$refs.table.refresh(true)
    },
    async loadData(parameter) {
      const requestParameters = Object.assign({}, { ...parameter }, { query: this.queryParam })
      return getDelayApplyList(requestParameters)
    },
  },
}
</script>

<style>
</style>
